{
 "cells": [
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "# PyTorch Tensor Basic Usage\n",
    "\n",
    "- Create Tensor\n",
    "- Indexing,Joining,Slicing\n",
    "- Initialization\n",
    "- Math Operations"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 1. Create Tensor\n",
    "### 1) random numbers"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 1,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 0.9161  0.7135  0.8969\n",
       " 0.7552  0.8760  0.2236\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch\n",
    "\n",
    "# torch.rand(sizes) -> [0,1)\n",
    "x = torch.rand(2,3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "-1.9352  0.9779  1.7401\n",
       " 2.0432 -0.1962 -0.1177\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 2,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.randn(sizes) -> Z(0,1)\n",
    "x = torch.randn(2,3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 3\n",
       " 1\n",
       " 4\n",
       " 0\n",
       " 2\n",
       "[torch.LongTensor of size 5]"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.randperm(n) -> permutation of 0~n\n",
    "x = torch.randperm(5)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2) zeros, ones, arange"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 0  0  0\n",
       " 0  0  0\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.zeros(2,3) -> [[0,0,0],[0,0,0]]\n",
    "x = torch.zeros(2,3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1  1  1\n",
       " 1  1  1\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.ones(2,3) -> [[0,0,0],[0,0,0]]\n",
    "x = torch.ones(2,3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 0.0000\n",
       " 0.5000\n",
       " 1.0000\n",
       " 1.5000\n",
       " 2.0000\n",
       " 2.5000\n",
       "[torch.FloatTensor of size 6]"
      ]
     },
     "execution_count": 6,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.arange(start,end,step=1) -> [start,end) with step\n",
    "x = torch.arange(0,3,step=0.5)\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3) Tensor Data Type"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 7,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "-7.4341e-17  4.5629e-41 -7.4341e-17\n",
       " 4.5629e-41 -7.4342e-17  2.9427e-44\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 7,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.FloatTensor(size or list)\n",
    "x = torch.FloatTensor(2,3)\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 2\n",
       " 3\n",
       "[torch.FloatTensor of size 2]"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.FloatTensor(size or list)\n",
    "x = torch.FloatTensor([2,3])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 2\n",
       " 3\n",
       "[torch.IntTensor of size 2]"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tensor.type_as(tensor_type)\n",
    "x = x.type_as(torch.IntTensor())\n",
    "x"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4) Numpy to Tensor, Tensor to Numpy"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1  2  3\n",
       " 4  5  6\n",
       "[torch.LongTensor of size 2x3]"
      ]
     },
     "execution_count": 10,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import numpy as np\n",
    "\n",
    "# torch.from_numpy(ndarray) -> tensor\n",
    "\n",
    "x1 = np.ndarray(shape=(2,3), dtype=int,buffer=np.array([1,2,3,4,5,6]))\n",
    "x2 = torch.from_numpy(x1)\n",
    "\n",
    "x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "array([[1, 2, 3],\n",
       "       [4, 5, 6]])"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tensor.numpy() -> ndarray\n",
    "x3 = x2.numpy()\n",
    "x3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5) Tensor on CPU & GPU"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1  2  3\n",
       " 4  5  6\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 12,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "x"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1  2  3\n",
       " 4  5  6\n",
       "[torch.cuda.FloatTensor of size 2x3 (GPU 0)]"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_gpu = x.cuda()\n",
    "x_gpu"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 14,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1  2  3\n",
       " 4  5  6\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 14,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "x_cpu = x_gpu.cpu()\n",
    "x_cpu"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 6) Tensor Size"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([10, 12, 3, 3])"
      ]
     },
     "execution_count": 15,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tensor.size() -> indexing also possible\n",
    "\n",
    "x = torch.FloatTensor(10,12,3,3)\n",
    "\n",
    "x.size()[:]"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 2. Indexing, Slicing, Joining, Reshaping\n",
    "### 1) Indexing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       "  0.9495  0.8593  0.2616\n",
       "  0.2279  0.0857  0.7603\n",
       "  0.4439  0.2024  0.8021\n",
       "  0.3477  0.6363  0.5002\n",
       " [torch.FloatTensor of size 4x3], \n",
       "  0.9495  0.8593  0.2616\n",
       "  0.3477  0.6363  0.5002\n",
       " [torch.FloatTensor of size 2x3])"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.index_select(input, dim, index)\n",
    "\n",
    "x = torch.rand(4,3)\n",
    "out = torch.index_select(x,0,torch.LongTensor([0,3]))\n",
    "\n",
    "x,out"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       "  0.9495\n",
       "  0.2279\n",
       "  0.4439\n",
       "  0.3477\n",
       " [torch.FloatTensor of size 4], \n",
       "  0.9495\n",
       "  0.8593\n",
       "  0.2616\n",
       " [torch.FloatTensor of size 3], \n",
       "  0.9495  0.8593\n",
       "  0.2279  0.0857\n",
       " [torch.FloatTensor of size 2x2])"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# pythonic indexing also works\n",
    "\n",
    "x[:,0],x[0,:],x[0:2,0:2]"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 18,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       " -2.0702 -0.3064 -0.8124\n",
       "  0.2870  0.2648  0.4418\n",
       " [torch.FloatTensor of size 2x3], \n",
       "  0  0  1\n",
       "  0  1  0\n",
       " [torch.ByteTensor of size 2x3], \n",
       " -0.8124\n",
       "  0.2648\n",
       " [torch.FloatTensor of size 2])"
      ]
     },
     "execution_count": 18,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.masked_select(input, mask)\n",
    "\n",
    "x = torch.randn(2,3)\n",
    "mask = torch.ByteTensor([[0,0,1],[0,1,0]])\n",
    "out = torch.masked_select(x,mask)\n",
    "\n",
    "x, mask, out"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2) Joining"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 19,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       "  1  2  3\n",
       "  4  5  6\n",
       " [torch.FloatTensor of size 2x3], \n",
       " -1 -2 -3\n",
       " -4 -5 -6\n",
       " [torch.FloatTensor of size 2x3], \n",
       "  1  2  3\n",
       "  4  5  6\n",
       " -1 -2 -3\n",
       " -4 -5 -6\n",
       " [torch.FloatTensor of size 4x3], \n",
       "  1  2  3 -1 -2 -3\n",
       "  4  5  6 -4 -5 -6\n",
       " [torch.FloatTensor of size 2x6])"
      ]
     },
     "execution_count": 19,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.cat(seq, dim=0) -> concatenate tensor along dim\n",
    "\n",
    "x = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "y = torch.FloatTensor([[-1,-2,-3],[-4,-5,-6]])\n",
    "z1 = torch.cat([x,y],dim=0)\n",
    "z2 = torch.cat([x,y],dim=1)\n",
    "\n",
    "x,y,z1,z2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "(0 ,.,.) = \n",
       "  1  2  3\n",
       "  4  5  6\n",
       "\n",
       "(1 ,.,.) = \n",
       "  1  2  3\n",
       "  4  5  6\n",
       "\n",
       "(2 ,.,.) = \n",
       "  1  2  3\n",
       "  4  5  6\n",
       "\n",
       "(3 ,.,.) = \n",
       "  1  2  3\n",
       "  4  5  6\n",
       "[torch.FloatTensor of size 4x2x3]"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.stack(sequence,dim=0) -> stack along new dim\n",
    "\n",
    "x = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "x_stack = torch.stack([x,x,x,x],dim=0)\n",
    "\n",
    "x_stack"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3) Slicing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       "  1  2  3\n",
       "  4  5  6\n",
       " -1 -2 -3\n",
       " -4 -5 -6\n",
       " [torch.FloatTensor of size 4x3], \n",
       "  1  2  3\n",
       "  4  5  6\n",
       " [torch.FloatTensor of size 2x3], \n",
       " -1 -2 -3\n",
       " -4 -5 -6\n",
       " [torch.FloatTensor of size 2x3], \n",
       "  1  2  3\n",
       "  4  5  6\n",
       " -1 -2 -3\n",
       " -4 -5 -6\n",
       " [torch.FloatTensor of size 4x3], \n",
       "  1\n",
       "  4\n",
       " -1\n",
       " -4\n",
       " [torch.FloatTensor of size 4x1], \n",
       "  2\n",
       "  5\n",
       " -2\n",
       " -5\n",
       " [torch.FloatTensor of size 4x1], \n",
       "  3\n",
       "  6\n",
       " -3\n",
       " -6\n",
       " [torch.FloatTensor of size 4x1])"
      ]
     },
     "execution_count": 21,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.chunk(tensor, chunks, dim=0) -> tensor into num chunks\n",
    "\n",
    "x_1, x_2 = torch.chunk(z1,2,dim=0)\n",
    "y_1, y_2, y_3 = torch.chunk(z1,3,dim=1)\n",
    "\n",
    "z1,x_1,x_2,z1,y_1,y_2,y_3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       "  1  2  3\n",
       "  4  5  6\n",
       " -1 -2 -3\n",
       " -4 -5 -6\n",
       " [torch.FloatTensor of size 4x3], \n",
       "  1  2  3\n",
       "  4  5  6\n",
       " [torch.FloatTensor of size 2x3], \n",
       " -1 -2 -3\n",
       " -4 -5 -6\n",
       " [torch.FloatTensor of size 2x3], (\n",
       "   1  2\n",
       "   4  5\n",
       "  -1 -2\n",
       "  -4 -5\n",
       "  [torch.FloatTensor of size 4x2], \n",
       "   3\n",
       "   6\n",
       "  -3\n",
       "  -6\n",
       "  [torch.FloatTensor of size 4x1]))"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.split(tensor,split_size,dim=0) -> split into specific size\n",
    "\n",
    "x1,x2 = torch.split(z1,2,dim=0)\n",
    "y1 = torch.split(z1,2,dim=1) \n",
    "\n",
    "z1,x1,x2,y1"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 4) squeezing"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([10, 1, 3, 1, 4]), torch.Size([10, 3, 4]))"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.squeeze(input,dim=None) -> reduce dim by 1\n",
    "\n",
    "x1 = torch.FloatTensor(10,1,3,1,4)\n",
    "x2 = torch.squeeze(x1)\n",
    "\n",
    "x1.size(),x2.size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([10, 3, 4]), torch.Size([1, 10, 3, 4]))"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.unsqueeze(input,dim=None) -> add dim by 1\n",
    "\n",
    "x1 = torch.FloatTensor(10,3,4)\n",
    "x2 = torch.unsqueeze(x1,dim=0)\n",
    "\n",
    "x1.size(),x2.size()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 5) Reshaping"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 42,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([10, 3, 4]),\n",
       " torch.Size([120]),\n",
       " torch.Size([5, 24]),\n",
       " torch.Size([3, 10, 4]))"
      ]
     },
     "execution_count": 42,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# tensor.view(size)\n",
    "\n",
    "x1 = torch.FloatTensor(10,3,4)\n",
    "x2 = x1.view(-1)\n",
    "x3 = x1.view(5,-1)\n",
    "x4 = x1.view(3,10,-1)\n",
    "\n",
    "x1.size(), x2.size(), x3.size(), x4.size()"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 3. Initialization"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       "  7.8079  3.7811  4.0829  8.0698\n",
       "  8.4885  5.2119  2.5769  1.4455\n",
       "  3.0500  3.9946  5.6066  4.9679\n",
       " [torch.FloatTensor of size 3x4], \n",
       " -0.4025 -0.2582 -0.0780 -0.2003\n",
       "  0.1577 -0.0933 -0.2640 -0.3402\n",
       "  0.1431 -0.1946 -0.1485  0.0026\n",
       " [torch.FloatTensor of size 3x4], \n",
       "  3.1415  3.1415  3.1415  3.1415\n",
       "  3.1415  3.1415  3.1415  3.1415\n",
       "  3.1415  3.1415  3.1415  3.1415\n",
       " [torch.FloatTensor of size 3x4])"
      ]
     },
     "execution_count": 26,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "import torch.nn.init as init\n",
    "\n",
    "x1 = init.uniform(torch.FloatTensor(3,4),a=0,b=9) \n",
    "x2 = init.normal(torch.FloatTensor(3,4),std=0.2)\n",
    "x3 = init.constant(torch.FloatTensor(3,4),3.1415)\n",
    "\n",
    "x1,x2,x3"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "## 4. Math Operations\n",
    "### 1) Arithmetic operations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       "  1  2  3\n",
       "  4  5  6\n",
       " [torch.FloatTensor of size 2x3], \n",
       "  1  2  3\n",
       "  4  5  6\n",
       " [torch.FloatTensor of size 2x3], \n",
       "   2   4   6\n",
       "   8  10  12\n",
       " [torch.FloatTensor of size 2x3], \n",
       "   2   4   6\n",
       "   8  10  12\n",
       " [torch.FloatTensor of size 2x3], \n",
       "  0  0  0\n",
       "  0  0  0\n",
       " [torch.FloatTensor of size 2x3])"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.add()\n",
    "\n",
    "x1 = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "x2 = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "add = torch.add(x1,x2)\n",
    "\n",
    "x1,x2,add,x1+x2,x1-x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       "  1  2  3\n",
       "  4  5  6\n",
       " [torch.FloatTensor of size 2x3], \n",
       "  11  12  13\n",
       "  14  15  16\n",
       " [torch.FloatTensor of size 2x3], \n",
       "  11  12  13\n",
       "  14  15  16\n",
       " [torch.FloatTensor of size 2x3], \n",
       "  1  2  3\n",
       "  4  5  6\n",
       " [torch.FloatTensor of size 2x3])"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.add() broadcasting\n",
    "\n",
    "x1 = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "x2 = torch.add(x1,10)\n",
    "\n",
    "x1,x2,x1+10,x2-10"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "  1   4   9\n",
       " 16  25  36\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 29,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.mul() -> size better match\n",
    "\n",
    "x1 = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "x2 = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "x3 = torch.mul(x1,x2)\n",
    "\n",
    "x3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 10  20  30\n",
       " 40  50  60\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 30,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.mul() -> broadcasting\n",
    "\n",
    "x1 = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "x2 = x1*10\n",
    "\n",
    "x2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 31,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 1  1  1\n",
       " 1  1  1\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 31,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.div() -> size better match\n",
    "\n",
    "x1 = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "x2 = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "x3 = torch.div(x1,x2)\n",
    "\n",
    "x3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 32,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       " 0.2000  0.4000  0.6000\n",
       " 0.8000  1.0000  1.2000\n",
       "[torch.FloatTensor of size 2x3]"
      ]
     },
     "execution_count": 32,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.div() -> broadcasting\n",
    "\n",
    "x1 = torch.FloatTensor([[1,2,3],[4,5,6]])\n",
    "\n",
    "x1/5"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 2) Other Math Operations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       " 1.00000e-33 *\n",
       "   5.5267  0.0000  5.5267  0.0000\n",
       "   0.0000  0.0000  5.5267  0.0000\n",
       "   0.0000  0.0000  0.0000  0.0000\n",
       " [torch.FloatTensor of size 3x4], \n",
       " 1.00000e-33 *\n",
       "   5.5267  0.0000  5.5267  0.0000\n",
       "   0.0000  0.0000  5.5267  0.0000\n",
       "   0.0000  0.0000  0.0000  0.0000\n",
       " [torch.FloatTensor of size 3x4])"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.pow(input,exponent)\n",
    "\n",
    "x1 = torch.FloatTensor(3,4)\n",
    "torch.pow(x1,2),x1**2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "  1   1   1   1\n",
       "inf   1 inf   1\n",
       "  1   1   1   1\n",
       "[torch.FloatTensor of size 3x4]"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.exp(tensor,out=None) \n",
    "\n",
    "x1 = torch.FloatTensor(3,4)\n",
    "torch.exp(x1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "     nan -92.8880 -86.5151     -inf\n",
       "    -inf     -inf     -inf     -inf\n",
       "    -inf     -inf     -inf     -inf\n",
       "[torch.FloatTensor of size 3x4]"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.log(input, out=None) -> natural logarithm\n",
    "\n",
    "x1 = torch.FloatTensor(3,4)\n",
    "torch.log(x1)"
   ]
  },
  {
   "cell_type": "markdown",
   "metadata": {},
   "source": [
    "### 3) Matrix operations"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\n",
       "-9.6088e-12  5.8988e-36  5.5267e-33  0.0000e+00 -9.6628e-12\n",
       " 2.4562e-18  6.9592e+27  2.4562e-18 -2.4818e+01  8.6738e-02\n",
       " 1.2129e-32  5.8988e-36  5.5267e-33  0.0000e+00 -9.6628e-12\n",
       "[torch.FloatTensor of size 3x5]"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.mm(mat1, mat2) -> matrix multiplication\n",
    "\n",
    "x1 = torch.FloatTensor(3,4)\n",
    "x2 = torch.FloatTensor(4,5)\n",
    "\n",
    "torch.mm(x1,x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "torch.Size([10, 3, 5])"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.bmm(batch1, batch2) -> batch matrix multiplication\n",
    "\n",
    "x1 = torch.FloatTensor(10,3,4)\n",
    "x2 = torch.FloatTensor(10,4,5)\n",
    "\n",
    "torch.bmm(x1,x2).size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "5.5267435454184226e-33"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.dot(tensor1,tensor2) -> dot product of two tensor\n",
    "\n",
    "x1 = torch.FloatTensor(3,4)\n",
    "x2 = torch.FloatTensor(3,4)\n",
    "\n",
    "torch.dot(x1,x2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 39,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       " 1.00000e-17 *\n",
       "  -7.4341  0.0000 -7.4341  0.0000\n",
       "   0.0000  0.0000  0.0000  0.0000\n",
       "  -7.4342  0.0000 -7.4342  0.0000\n",
       " [torch.FloatTensor of size 3x4], \n",
       " 1.00000e-17 *\n",
       "  -7.4341  0.0000 -7.4342\n",
       "   0.0000  0.0000  0.0000\n",
       "  -7.4341  0.0000 -7.4342\n",
       "   0.0000  0.0000  0.0000\n",
       " [torch.FloatTensor of size 4x3])"
      ]
     },
     "execution_count": 39,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.t(matrix) -> transposed matrix\n",
    "\n",
    "x1 = torch.FloatTensor(3,4)\n",
    "\n",
    "x1,x1.t()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 40,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(torch.Size([10, 3, 4]), torch.Size([10, 4, 3]), torch.Size([10, 4, 3]))"
      ]
     },
     "execution_count": 40,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.transpose(input,dim0,dim1) -> transposed matrix\n",
    "\n",
    "x1 = torch.FloatTensor(10,3,4)\n",
    "\n",
    "x1.size(), torch.transpose(x1,1,2).size(), x1.transpose(1,2).size()"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 41,
   "metadata": {
    "collapsed": false
   },
   "outputs": [
    {
     "data": {
      "text/plain": [
       "(\n",
       " 1.00000e-18 *\n",
       "   0.0000  0.0000 -0.0008  0.0000\n",
       "  -0.0008  0.0000  0.0000  0.0000\n",
       "   0.0000  0.0000 -4.6391  0.0000\n",
       "  -0.0008  0.0000 -0.0005  0.0000\n",
       " [torch.FloatTensor of size 4x4], (\n",
       "  1.00000e-18 *\n",
       "   -0.0000  0.0000\n",
       "   -0.0000 -0.0000\n",
       "   -4.6391  0.0000\n",
       "    0.0000  0.0000\n",
       "  [torch.FloatTensor of size 4x2], \n",
       "  -0.0000 -0.0000 -0.0000 -0.0000\n",
       "   0.7071  0.0000 -1.0000 -1.0000\n",
       "   0.0000 -0.0000 -0.0000  0.0000\n",
       "   0.7071  0.0000 -0.0000  0.0000\n",
       "  [torch.FloatTensor of size 4x4]))"
      ]
     },
     "execution_count": 41,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "# torch.eig(a,eigenvectors=False) -> eigen_value, eigen_vector\n",
    "\n",
    "x1 = torch.FloatTensor(4,4)\n",
    "\n",
    "x1,torch.eig(x1,True)"
   ]
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Python 3",
   "language": "python",
   "name": "python3"
  },
  "language_info": {
   "codemirror_mode": {
    "name": "ipython",
    "version": 3
   },
   "file_extension": ".py",
   "mimetype": "text/x-python",
   "name": "python",
   "nbconvert_exporter": "python",
   "pygments_lexer": "ipython3",
   "version": "3.5.2"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 1
}
